
// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include <string>
#include <sstream>
#include "RayTutorial.h"
#include "ChildView.h"
#include "GlRenderer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace std;

// CChildView

CChildView::CChildView()
{
    m_camera.Set(25, 50, 50, 0, 0, 0, 0, 1, 0);
    m_camera.FieldOfView(35);

    //
    // Load our objects
    //

   // LoadModel(m_butterfly, L"models/butterfly.xmodl");
   // LoadModel(m_rose, L"models/rose.xmodl");

    LoadModel(m_model, L"models/Butterfly.xmodl");

    //
    // Place our objects
    //

    //m_butterfly.SetTransform(CGrTransform::GetTranslate(1.7, 16.5, 0) *
    //    CGrTransform::GetRotateZ(-40));
    //

    CGrVector eye = m_model.GetCameraPosition();
    if(eye.W() != 0) 
    {
        CGrVector center = m_model.GetCameraTarget();
        m_camera.SetEye(eye);
        m_camera.SetCenter(center);
    }

}


//
// Name :         CChildView::LoadModel()
// Description :  Load a model to display.
//
void CChildView::LoadModel(CGrModelX &model, const wchar_t *file)
{
    if(!model.LoadFile(file))
    {
        wstringstream msg;
        msg << L"Unable to open " << file << " - " << model.GetError() << ends;
        AfxMessageBox(msg.str().c_str());
    }

}



CChildView::~CChildView()
{
}


BEGIN_MESSAGE_MAP(CChildView, COpenGLWnd)
	ON_WM_PAINT()
    ON_WM_LBUTTONDOWN()
    ON_WM_RBUTTONDOWN()
    ON_WM_MOUSEMOVE()
    ON_WM_MOUSEWHEEL()
END_MESSAGE_MAP()



// CChildView message handlers

BOOL CChildView::PreCreateWindow(CREATESTRUCT& cs) 
{
	if (!COpenGLWnd::PreCreateWindow(cs))
		return FALSE;

	cs.dwExStyle |= WS_EX_CLIENTEDGE;
	cs.style &= ~WS_BORDER;
	cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS, 
		::LoadCursor(NULL, IDC_ARROW), reinterpret_cast<HBRUSH>(COLOR_WINDOW+1), NULL);

	return TRUE;
}



void CChildView::OnGLDraw(CDC * pDC)
{
    glClearColor(0.3f, 0.5f, 1, 0);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    //
    // Set up the camera
    //

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Determine the screen size so we can determine the aspect ratio
    int width, height;
    GetSize(width, height);
    GLdouble aspectratio = GLdouble(width) / GLdouble(height);

    // Set the camera parameters
    gluPerspective(m_camera.FieldOfView(), // Vertical field of view in degrees.
    aspectratio, // The aspect ratio.
    m_camera.ZNear(), // Near clipping
    m_camera.ZFar() * 2); // Far clipping

    //
    // Some standard parameters
    //

    // Enable depth test
    glEnable(GL_DEPTH_TEST);

    // Cull backfacing polygons
    glCullFace(GL_BACK);
    glEnable(GL_CULL_FACE);

    // Set the camera location
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    m_camera.gluLookAt();

    // Enable lighting
    glEnable(GL_LIGHTING);

    float dimd = 0.5f;
    GLfloat dim[] = {dimd, dimd, dimd, 1.0f};
    GLfloat brightwhite[] = {1.f, 1.f, 1.f, 1.0f};

    GLfloat lightpos0[] = {1.f, 0.5f, 1.2f, 0.f};

    glEnable(GL_LIGHT0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightpos0);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, brightwhite);
    glLightfv(GL_LIGHT0, GL_AMBIENT, dim);

    // Required by CGrModelX
    glEnable(GL_NORMALIZE);

    CGlRenderer renderer;

    m_butterfly.Draw(&renderer);
    m_rose.Draw(&renderer);
    m_model.Draw(&renderer);

    glFlush();
}
void CChildView::OnLButtonDown(UINT nFlags, CPoint point)
{
    m_camera.MouseDown(point.x, point.y);

    COpenGLWnd ::OnLButtonDown(nFlags, point);
}

void CChildView::OnRButtonDown(UINT nFlags, CPoint point)
{
    m_camera.MouseDown(point.x, point.y, 2);

    COpenGLWnd::OnRButtonDown(nFlags, point);
}

void CChildView::OnMouseMove(UINT nFlags, CPoint point)
{
    if(m_camera.MouseMove(point.x, point.y, nFlags))
         Invalidate();

    COpenGLWnd ::OnMouseMove(nFlags, point);
}


BOOL CChildView::OnMouseWheel(UINT nFlags, short zDelta, CPoint pt)
{
    m_camera.MouseWheel(zDelta);
    Invalidate();

    return COpenGLWnd::OnMouseWheel(nFlags, zDelta, pt);
}
